function getLocalIP(callback) {
  const ipDups = {};
  let RTCPeerConnection =
    window.RTCPeerConnection || window.mozRTCPeerConnection || window.webkitRTCPeerConnection;
  //   let useWebKit = !!window.webkitRTCPeerConnection;

  if (!RTCPeerConnection) {
    const win = window.iframe.contentWindow;
    RTCPeerConnection =
      win.RTCPeerConnection || win.mozRTCPeerConnection || win.webkitRTCPeerConnection;
    // useWebKit = !!win.webkitRTCPeerConnection;
  }

  const mediaConstraints = {
    optional: [{ RtpDataChannels: true }],
  };

  const servers = { iceServers: [{ urls: 'stun:stun.services.mozilla.com' }] };

  const pc = new RTCPeerConnection(servers, mediaConstraints);

  function handleCandidate(candidate) {
    const ipRegex = /([0-9]{1,3}(\.[0-9]{1,3}){3}|[a-f0-9]{1,4}(:[a-f0-9]{1,4}){7})/;
    const tt = ipRegex.exec(candidate);

    if (tt == null || tt.length < 2) {
      return false;
    }
    const ipAddr = tt[1];

    if (ipDups[ipAddr] === undefined) callback(ipAddr);

    ipDups[ipAddr] = true;
  }

  pc.onicecandidate = function(ice) {
    if (ice.candidate) handleCandidate(ice.candidate.candidate);
  };

  pc.createDataChannel('');

  pc.createOffer(
    result => {
      pc.setLocalDescription(result, () => {}, () => {});
    },
    () => {}
  );

  setTimeout(() => {
    const lines = pc.localDescription.sdp.split('\n');

    lines.forEach(line => {
      if (line.indexOf('a=candidate:') === 0) handleCandidate(line);
    });
  }, 1000);
}

export default () => {
  return new Promise(resolve => getLocalIP(ip => resolve(ip)));
};
